/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Acceso;

import Modelo.DAL.Personal_DAO;
import Modelo.PersonalDsi;
import Modelo.PerteneceGrupo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.json.JSONObject;

/**
 *
 * @author alb3rto
 */
public class recordarPasswordAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        recordarPasswordForm formBean = (recordarPasswordForm) form;
        ActionErrors errors = new ActionErrors();
        boolean error = false;

        String nacionalidad = formBean.getNacionalidad();
        String ci = formBean.getCedula();
        String login = formBean.getUsuario();
        String nombre1 = formBean.getNombre1();
        String apellido1 = formBean.getApellido1();

        if (ci == null || ci.length() < 1) {
            errors.add("falta_Cedula", new ActionMessage("errors.falta_Cedula"));
            saveErrors(request, errors);
            error = true;
            System.out.println("\n\nno ceudla");
        } else if (!FormatUtility.validatePattern(ci, 2)) {
            errors.add("invalid_Cedula", new ActionMessage("errors.invalid_Cedula"));
            saveErrors(request, errors);
            error = true;
            System.out.println("\n\nmal cedula");
        }

        // Validamos que el login usb no sea nulo
        if (login == null || login.length() < 1) {
            errors.add("falta_login", new ActionMessage("errors.falta_login"));
            saveErrors(request, errors);
            error = true;
            System.out.println("\n\nlogin null");
        }

        // validamos el primer nombre
        if (nombre1 == null || nombre1.length() < 1) {
            errors.add("falta_Nombre", new ActionMessage("errors.falta_Nombre"));
            saveErrors(request, errors);
            error = true;
            System.out.println("\n\nNO nombre1");
        } else if (nombre1.length() < 2) {
            errors.add("long_Nombre", new ActionMessage("errors.long_Nombre"));
            saveErrors(request, errors);
            error = true;
        } else if (!FormatUtility.validatePattern(nombre1, 3)) {
            errors.add("invalid_caracter_N1", new ActionMessage("errors.invalid_caracter_N1"));
            saveErrors(request, errors);
            error = true;
            System.out.println("\n\nmal nombre1");
        }
        // validamos el primer apellido
        if (apellido1 == null || apellido1.length() < 1) {
            errors.add("falta_Apellido", new ActionMessage("errors.falta_Apellido"));
            saveErrors(request, errors);
            error = true;
        } else if (apellido1.length() < 2) {
            errors.add("long_Apellido", new ActionMessage("errors.long_Apellido"));
            saveErrors(request, errors);
            error = true;
            System.out.println("\n\nno apellido1");
        } else if (!FormatUtility.validatePattern(apellido1, 3)) {
            errors.add("invalid_caracter_A1", new ActionMessage("errors.invalid_caracter_A1"));
            saveErrors(request, errors);
            error = true;
            System.out.println("\n\nmal apellido1");
        }

        if (!Personal_DAO.personalExiste(nacionalidad + "-" + ci)) {
            error = true;
            errors.add("persona_no_Existe", new ActionMessage("errors.persona_no_Existe"));
            saveErrors(request, errors);
            System.out.println("\n\npesoan no existe");
        }
        if (!Personal_DAO.loginExiste(login)) {
            error = true;
            errors.add("login_no_Existe", new ActionMessage("errors.login_no_Existe"));
            saveErrors(request, errors);
            System.out.println("\n\nlogin no existe");
        }

        if (error) {
            System.out.println("\n\nojo ERROR");
            errors.add("passwordNoRecordado", new ActionMessage("errors.passwordNoRecordado"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }

        PersonalDsi pDSI = new PersonalDsi();
        pDSI.setCi(nacionalidad + "-" + ci);
        pDSI.setNombre1(nombre1);
        pDSI.setApellido1(apellido1);
        //pDSI.setUsbid(usbid);
        pDSI.setLogin(login);
        pDSI.setPassword(FormatUtility.md5(ci));

        PerteneceGrupo pg = new PerteneceGrupo();

        System.out.println("\n\nNO ERROR");

        JSONObject personal = Personal_DAO.consultarEmpleados(pDSI, pg, "", "", "", "");
        if (personal != null && personal.has("tam") && personal.get("tam").equals("1")) {
            System.out.println("\n\nEXISTE");
            error = Personal_DAO.resetPassword(pDSI);
            if (!error) {
                System.out.println("\n\nNO ERROR PWD recordado");
                return mapping.findForward(SUCCESS);
            } else {
                System.out.println("\n\nERROR PWD no recordado");
            }
        }

        System.out.println("\n\nERROR ERROR ");
        errors.add("passwordNoRecordado", new ActionMessage("errors.passwordNoRecordado"));
        saveErrors(request, errors);
        return mapping.findForward(FAILURE);
    }
}
